﻿using Newtonsoft.Json;
using PHMEE.Admin;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PHMEE.MqttClient.MqttBusinessCommand
{
    public class WaveDataCommand : BaseCommand
    {
        private IMqttAppService _mqttAppService;

        public WaveDataCommand(IMqttAppService mqttAppService)
        {
            _mqttAppService = mqttAppService;
        }
        public override async Task ExecuteAsync(string requestInfo)
        {
            Console.WriteLine($"=====我是处理采集站【{SN}】-【WaveData业务:{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff")}】====");
            await Task.Run(async () =>
            {

                WaveDataReqDto data = null;
                try
                {
                    data = JsonConvert.DeserializeObject<WaveDataReqDto>(requestInfo)!;
                    if (data != null)
                    {
                        //CRC校验数据是否完整
                        if (data.Content.GetCRC32() != data.CRC)
                        {
                            Status = -1;
                            return;
                        }
                        //插入数据
                        await _mqttAppService.InsertWaveData(requestInfo, SN);
                        string content = JsonConvert.SerializeObject(data);
                        string fileName = $@"WaveData_{DateTime.Now.ToString("yyyy-MM-ddTHH-mm-ss")}.txt";
                        //如果接收到回复
                        if (data.PackIndex == data.PackCount - 1)
                        {
                            isReplay = true;
                            ResPonsePayload = new WaveDataRespDto();
                        }
                        //生成文件
                        WriteFileHelper.WriteFile(SN, fileName, requestInfo);
                    }
                }
                catch
                {
                    Status = -1;
                    return;
                }
                #region 分包时使用

                //((WaveDataRespDto)ResPonsePayload).PackIndex = data.PackIndex;
                //((WaveDataRespDto)ResPonsePayload).Id = data.Id;
                ////拼包结果，现在整包
                //var result = _clientCenter.AddPackToCache<WaveDataReqDto, byte>(this, data, key: GetType().Name + SN + data.Id + data.Time.ToString("yyyy-MM-ddTHH:mm:ss:fff") + data.Code);
                //if (!result.IsComplete)
                //{
                //    return;
                //}
                //// 数据接收完毕
                //var completeWave = result.DataModel;//完整数据

                ////进行CRC比较
                //var CRC = completeWave.Content.GetCRC32();
                //if (CRC != data.CRC)
                //{
                //    string key = GetType().Name + SN + data.Id;
                //    var cacheData = new List<WaveDataReqDto>();
                //    _clientCenter.AddOrUpdateTemporaryObject<List<WaveDataReqDto>>(key, cacheData);
                //    Status = -1;
                //    return;
                //}
                #endregion
            });
        }
    }
}
